package wli.java.file.util;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class FileWriterUtil {
	private String file_name = null;
	private ObjectOutputStream objectOutput;
	private BufferedOutputStream bufOutput;
	private StringBuffer file_containt;

	public void setFileName(String file_name) {
		this.file_name = file_name;
	}
	
	public void setFileName(File file_name) {
		this.file_name = file_name.getPath();
	}
	public StringBuffer getFileContaint() {
		return file_containt;
	}

	public void setFileContaint(StringBuffer file_containt) {
		this.file_containt = file_containt;
	}
	public void setFileContaint(String file_containt) {
		this.file_containt = new StringBuffer(file_containt);
	}
	
	public void writeFileUseObjectOutputStream(String inputString) {
		String temp = inputString;

		try {
			objectOutput = new ObjectOutputStream(new FileOutputStream(
					file_name));
			objectOutput.writeObject(temp);
			objectOutput.close();
		} catch (IOException e) {

		}
	}

	public void writeFileUseDataOutputStream(String inputString) {
		String temp = inputString;
		try {
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(
					file_name));
			// dos.writeChars(temp); //16 bits
			dos.writeBytes(temp); // 8 bits
			dos.close();
		} catch (IOException e) {

		}
	}

	public void writeFileUseFileOutputStream(String inputString) {
		String temp = inputString;

		try {

			FileOutputStream fos = new FileOutputStream(file_name);
			for (int i = 0; i < temp.length(); i++) {
				fos.write(temp.charAt(i));
			}

			fos.close();
		} catch (IOException e) {

		}
	}

	public void writeFileUseBufferedOutputStream(String inputString) {
		String temp = inputString;

		try {
			bufOutput = new BufferedOutputStream(
					new FileOutputStream(file_name));
			for (int i = 0; i < temp.length(); i++) {
				bufOutput.write(temp.charAt(i));
			}
			bufOutput.flush();
			bufOutput.close();
		} catch (IOException e) {

		}
	}

	public void writeFileUseBufferedWriter(String inputString) {
		String temp = inputString;

		try {
			FileWriter fw = new FileWriter(file_name);
			BufferedWriter bfw = new BufferedWriter(fw);
			bfw.write(temp);
			bfw.flush();
			bfw.close();
		} catch (IOException e) {

		}
	}
}
